/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simplerecipes.model;

/**
 *
 * @author matamh0a
 */
public class Ingredient implements Comparable<Ingredient> {
    int id;
    String name;
    String alternativeUnit;
    Unit unit;
    IngredientType type;
    double weight; //in grams
    double calories; //calories per unit.
    double cost;

    public Ingredient(int id, String name, Unit unit, double weight, double calories) {
        this.id = id;
        this.name = name;
        this.unit = unit;
        this.weight = weight;
        this.calories = calories;
        this.type = IngredientType.OTHER;
    }
    
    public Ingredient(int id, String name, Unit unit, double weight, 
            double calories, IngredientType type) {
        this(id, name, unit, weight, calories);
        this.type = type;
    }

    public Ingredient(int id, String name, String unit, double weight, 
            double calories, IngredientType type) {
        this.id = id;
        this.name = name;
        this.weight = weight;
        this.calories = calories;
        this.type = IngredientType.OTHER;
        this.type = type;
        try {
            this.unit = Unit.valueOf(unit);
        } catch (IllegalArgumentException ex){
            this.unit = Unit.cup;
            alternativeUnit = unit;
        }
    }
    
    public Ingredient(int id, String name, String unit, double weight, 
            double calories, IngredientType type, double cost) {
        this(id, name, unit, weight, calories, type);
        this.cost = cost;
    }

    public double getCalories(Unit u) {
        //normal weights
        switch(u) {
            case g:
                return calories/weight;
            case kg:
                return calories/weight * Unit.KG_TO_G;
            case oz:
                return calories/weight * Unit.OZ_TO_G;
            case lb:
                return calories/weight * Unit.LB_TO_G;
        }
            
        
        //convert ingredient unit to cup.
        double factor = 1;
        switch(unit) {
            case cup:
                factor = 1;
                break;
            case tbsp:
                factor = Unit.CUP_TO_TBSP;
                break;
            case tsp:
                factor = Unit.CUP_TO_TSP;
                break;
        }
        
        switch(u) {
            case cup:
                return calories * factor / 1;
            case tbsp:
                return calories * factor / Unit.CUP_TO_TBSP;
            case tsp:
                return calories * factor / Unit.CUP_TO_TSP;
        }
        
        return 0;
    }
    
    public double getCost(Unit u) {
        //normal weights
        switch(u) {
            case g:
                return cost/weight;
            case kg:
                return cost/weight * Unit.KG_TO_G;
            case oz:
                return cost/weight * Unit.OZ_TO_G;
            case lb:
                return cost/weight * Unit.LB_TO_G;
        }
            
        
        //convert ingredient unit to cup.
        double factor = 1;
        switch(unit) {
            case cup:
                factor = 1;
                break;
            case tbsp:
                factor = Unit.CUP_TO_TBSP;
                break;
            case tsp:
                factor = Unit.CUP_TO_TSP;
                break;
        }
        
        switch(u) {
            case cup:
                return cost * factor / 1;
            case tbsp:
                return cost * factor / Unit.CUP_TO_TBSP;
            case tsp:
                return cost * factor / Unit.CUP_TO_TSP;
        }
        
        return 0.0;
    }
    
    public double getWeight(Unit u) {
        //normal weights
        switch(u) {
            case g:
                return 1;
            case kg:
                return Unit.KG_TO_G;
            case oz:
                return Unit.OZ_TO_G;
            case lb:
                return Unit.LB_TO_G;
        }
            
        
        //convert ingredient unit to cup.
        double factor = 1;
        switch(unit) {
            case cup:
                factor = 1;
                break;
            case tbsp:
                factor = Unit.CUP_TO_TBSP;
                break;
            case tsp:
                factor = Unit.CUP_TO_TSP;
                break;
        }
        
        switch(u) {
            case cup:
                return weight * factor / 1;
            case tbsp:
                return weight * factor / Unit.CUP_TO_TBSP;
            case tsp:
                return weight * factor / Unit.CUP_TO_TSP;
        }
        
        return 0;
    }
    
    public double getCalories() {
        return calories;
    }

    public void setCalories(double calories) {
        this.calories = calories;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Unit getUnit() {
        return unit;
    }

    public void setUnit(Unit unit) {
        this.unit = unit;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        //return "Ingredient{" + "id=" + id + ", name=" + name + ", unit=" + unit + ", type=" + type + ", weight=" + weight + ", calories=" + calories + '}';
        return name;
    }

    public IngredientType getType() {
        return type;
    }

    public void setType(IngredientType type) {
        this.type = type;
    }

    public int compareTo(Ingredient o) {
        return name.compareTo(o.name);
    }

    public String getAlternativeUnit() {
        return alternativeUnit;
    }

    public void setAlternativeUnit(String alternativeUnit) {
        this.alternativeUnit = alternativeUnit;
    }

    public double getCost() {
        return cost;
    }

    public void setCost(double cost) {
        this.cost = cost;
    }
}
